package qscript;

import org.qscript.Argument;
import org.qscript.Script;
import org.qscript.Token;
import org.qscript.errors.EvaluationException;
import org.qscript.operator.Operator;

public class HypotenuseFunction extends Operator {

	public HypotenuseFunction(String symbol, int nbrArgsNeeded, int priority, int type) {
		super(symbol, nbrArgsNeeded, priority, type);
	}

	@Override
	public Argument resolve(Script script, Token token, Argument[] args,
			Object... objects) throws EvaluationException {
		testForUninitialisedVars(script, args);
		Argument a0 = args[0];
		Argument a1 = args[0];
		if(a0.isNumeric && a1.isNumeric){
			double s0 = a0.toDouble(), s1 = a1.toDouble();
			double hyp = Math.sqrt(s0*s0 + s1*s1);
			script.fireEvent(HpotCalculatedEvent.class, null, token, new Argument(new Double(hyp)));
			return new Argument(new Double(hyp));
		}

		// If we get here then the arguments are invalid
		handleInvalidArguments(script, token);
		return null;
	}

}
